package com.doit.mr.day04.itera;

import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.Iterator;

/**
 * @DATE 2021/12/8/21:40
 * @Author MDK
 * @Version 2021.2.2
 **/
public class MyIterator implements Iterator<MyUser> {
    BufferedReader br = null;
    {
        try {
            br = new BufferedReader(new FileReader("d://aaa//1.txt"));
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }
    }

    String line = null;
    //判断是否还有下一条数据
    @Override
    public boolean hasNext() {
        boolean b = false;
        try {
            line = br.readLine();
            if(line != null){
                b = true;
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        return b;
    }

    //一个对象就可以迭代无数条数据
    MyUser myUser = new MyUser();
    //如果有下一条数据  获取该数据
    @Override
    public MyUser next() {
        String[] split = line.split(",");
        //1,张飞,21,北京,80.0
        String uid = split[0];
        String name = split[1];
        int age = Integer.parseInt(split[2]);
        String city = split[3];
        double score = Double.parseDouble(split[4]);

        myUser.setUid(uid);
        myUser.setName(name);
        myUser.setAge(age);
        myUser.setCity(city);
        myUser.setScore(score);
        return myUser;
    }
}
